//
//  SMTableIndexview.m
//  TBIndexView
//
//  Created by waw on 2018/2/8.
//  Copyright © 2018年 极客天地. All rights reserved.
//

#import "SMTableIndexview.h"

static CGFloat TopAndBottomMargin = 10;
static CGFloat VerticalMargin = 10;

@interface SMTableIndexview()

@property(nonatomic,strong) SMTableindexviewconfig * config;

@property(nonatomic,strong) UIView * leftView;
@property(nonatomic,strong) UIView * rightView;

@end

@implementation SMTableIndexview

-(instancetype)initWithFrame:(CGRect)frame andConfig:(SMTableindexviewconfig *)config{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        self.config = config;
        [self setUpUI];
    }
    return self;
    
}

-(void) setUpUI{
    
    [self setUpLeftUI];
    [self setUPRightUI];
    
}

-(void) setUpLeftUI{
    
    
    if(self.config.groupTitleArray.count == 0){
        return;
    }
    
    CGRect lframe = CGRectMake(0, 0, self.config.leftIndexviewWidth, self.bounds.size.height);
    self.leftView = [[UIView alloc]initWithFrame:lframe];
    self.leftView.backgroundColor = [UIColor redColor];
    [self addSubview:self.leftView];
    
    NSInteger count = self.config.groupTitleArray.count;
    
    CGFloat titleLabelH = (self.leftView.bounds.size.height - ((count-1) *VerticalMargin) - TopAndBottomMargin * 2) / count;
    
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = self.config.leftIndexviewWidth;
    CGFloat h = titleLabelH;
    for(int i = 0 ;i < count; i++){
        UILabel * label = [[UILabel alloc]init];
        
        y = ( h + VerticalMargin) * i + TopAndBottomMargin;
        
        label.frame = CGRectMake(x, y, w, h);
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        label.tag = i + 100;
        
        NSString * title = self.config.groupTitleArray[i];
        label.text = title;
        [self.leftView addSubview:label];
    }
    
    UITapGestureRecognizer  *tapGest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(leftViewTapAction:)];
    [self.leftView addGestureRecognizer:tapGest];
 
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    NSLog(@"滑动事件");

}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
}
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    
    
}

-(void) leftViewTapAction:(UITapGestureRecognizer*)gest{
    
    NSLog(@"点击事件");
    
}

-(void) leftViewPanAction:(UIPanGestureRecognizer*)gest{
    
    
}


-(void) setUPRightUI{
    
    
}


@end
